from flask import Blueprint, render_template, request
from .admin_manager import AdminManager  # 导入 AdminManager 类

admin_bp = Blueprint('admin', __name__)

# 实例化 AdminManager
admin_manager = AdminManager()

@admin_bp.route('/adminRestList', methods=['GET', 'POST'])
def admin_rest_list_page():
    if request.method == 'GET':
        # 获取餐厅列表
        restaurants = admin_manager.get_restaurant_list()
        if restaurants:
            return render_template('adminRestList.html', result=restaurants, messages="done")
        else:
            return render_template('adminRestList.html', messages="none")
    elif request.form.get("action") == "移除":
        # 删除餐厅
        rest_name = request.form.get('RESTName')
        success = admin_manager.delete_restaurant(rest_name)
        if success:
            return render_template('adminRestList.html', messages="delete")
        else:
            return render_template('adminRestList.html', messages="fail")

@admin_bp.route('/adminCommentList', methods=['GET', 'POST'])
def admin_comment_page():
    if request.method == 'GET':
        # 获取评论列表
        comments = admin_manager.get_comment_list()
        if comments:
            return render_template('adminCommentList.html', result=comments, messages="done")
        else:
            return render_template('adminCommentList.html', messages="none")
    elif request.form.get("action") == "按评分升序排列":
        # 按评分排序评论
        sorted_comments = admin_manager.get_sorted_comments()
        if sorted_comments:
            return render_template('adminCommentList.html', result=sorted_comments, messages="done")
        else:
            return render_template('adminCommentList.html', messages="none")